package com.zy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.bean.bo.chart.ChartRecordsBo;
import com.zy.bean.po.ChartRecords;
import com.zy.bean.vo.FlowChartVo;
import com.zy.config.SpringContextHolder;
import com.zy.enums.ActveEnum;
import com.zy.mapper.ChartRecordsMapper;
import com.zy.records.VersionRecord;
import com.zy.service.ChartRecordsService;
import com.zy.util.SnowflakeIdWorker;
import com.zy.util.VersionUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

import static org.fatewa.engine.genius.utils.GenericUtils.transaction;

/**
 * @author hanhuafeng
 * @createDate 2023-07-19 12:38:39
 * @description 流程图记录表
 */
@Service
@RequiredArgsConstructor

public class ChartRecordsServiceImpl extends ServiceImpl<ChartRecordsMapper, ChartRecords>
        implements ChartRecordsService {
    private final VersionUtil versionUtil;

    /**
     * 根据项目code获取流程图列表
     *
     * @param projectCode 项目code
     * @return List<FlowChartVo> 流程图列表
     */
    @Override
    public List<FlowChartVo> getChartRecordsByProjectCode(String projectCode) {
        List<FlowChartVo> result = new LinkedList<>();
        List<ChartRecords> chartRecordsList = lambdaQuery().eq(ChartRecords::getProjectCode, projectCode).list();
        if (ObjectUtils.isEmpty(chartRecordsList)) {
            return result;
        }
        chartRecordsList.forEach(chartRecords -> {
            FlowChartVo flowChartVo = new FlowChartVo();
            flowChartVo.setCode(chartRecords.getCode());
            flowChartVo.setName(chartRecords.getName());
            flowChartVo.setDescribe(chartRecords.getDescribe());
            flowChartVo.setIsActive(chartRecords.getIsActive());
            flowChartVo.setCreateTime(chartRecords.getCreateTime());
            flowChartVo.setUpdateTime(chartRecords.getUpdateTime());
            flowChartVo.setProjectCode(chartRecords.getProjectCode());
            flowChartVo.setActiveVersionInfo(versionUtil.getVersionByCode(chartRecords.getActiveVersionCode()));
            flowChartVo.setVersionList(versionUtil.getVersionListByProjectCode(chartRecords.getCode()));
            result.add(flowChartVo);
        });
        return result;
    }

    @Override
    public void deleteChartRecordsByProjectCode(String projectCode) {
        LambdaQueryWrapper<ChartRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChartRecords::getProjectCode, projectCode);
        this.remove(lambdaQueryWrapper);
    }

    /**
     * 创建流程图记录
     *
     * @param chartRecordsBo 流程图记录
     * @return ChartRecords 流程图记录
     */
    @Override
    public ChartRecords createChartRecords(ChartRecordsBo chartRecordsBo) {
        String chartCode = SnowflakeIdWorker.getUuid();
        ChartRecords chartRecords = new ChartRecords();
        transaction((manager, status) -> {
            String versionCode = versionUtil.createVersion(VersionRecord.INIT_VERSION_ENUM.versionName(), VersionRecord.INIT_VERSION_ENUM.desc(), chartCode);
            chartRecords.setCode(chartCode);
            chartRecords.setName(chartRecordsBo.getName());
            chartRecords.setDescribe(chartRecordsBo.getDescribe());
            if (ObjectUtils.isNotEmpty(chartRecordsBo.getIsActive())) {
                chartRecords.setIsActive(chartRecordsBo.getIsActive());
            } else {
                chartRecords.setIsActive(ActveEnum.NOT_ACTIVE.getValue());
            }
            chartRecords.setProjectCode(chartRecordsBo.getProjectCode());
            chartRecords.setActiveVersionCode(versionCode);
            this.save(chartRecords);
        }, SpringContextHolder.getApplicationContext());
        return chartRecords;
    }
}




